﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDF.DataHelper.BussinessObject;
using System.Data;
using MDF.DataHelper.DataObject;
using MDF.Utilites.Tools;
using MDF.DataHelper.BussinessObject.UsersBO;
using MDF.DataHelper.DataObject.UsersDO;

namespace MDF.DataHelper.BussinessLogic.UsersBL
{
    public class UserProfileBUS : BaseBussinessLogic
    {
        #region staticMember
        /// <summary>
        /// 
        /// </summary>
        private static UserProfileBUS _instance;
        /// <summary>
        /// 
        /// </summary>
        public static UserProfileBUS Current
        {
            get { return _instance == null ? _instance = new UserProfileBUS() : _instance; }
        }
        #endregion

        public UserProfile Get(string username, string password)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetByUsernamePassword(username, password);
            if (dt == null || dt.Rows.Count == 0)
                return null;
            var lstTemp = dt.ToObject<UserProfile>();
            return lstTemp;
        }

        public UserProfile Get(string username)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetByUsername(username);
            var lstTemp = dt.ToObject<UserProfile>();
            return lstTemp;
        }

        public UserProfile Get(int userId)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetByUserID(userId);
            var lstTemp = dt.ToObject<UserProfile>();
            return lstTemp;
        }

        public UserProfile Login(string Username, string Password)
        {
            var userMembership = UserMembershipBUS.Current.GetByUsername(Username);
            if (userMembership == null)
                return null;
            Password = MDF.Utilites.Tools.CommonMethods.Encrypt(Password + userMembership.PasswordSalt);
            var user = this.Get(Username, Password);
            if (user == null)
                return null;
            user.Roles = UserInRolesBUS.Current.GetByUserID(user.UserId).Select(m => m.RoleName).ToList();
            user.Groups = UserInGroupsBUS.Current.GetByUserID(user.UserId).Select(m => m.GroupName).ToList();
            user.Childs = this.GetChilds(user.UserId).Select(m => m.UserId).ToList();
            return user;
        }

        private List<UserProfile> GetChilds(int UserID)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetChilds(UserID);
            var lstTemp = dt.ToList<UserProfile>();
            return lstTemp;
        }

        public List<UserProfile> GetList(string Keyword,string lstUserID, int Page, int PageSize)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetList(Keyword, lstUserID, Page, PageSize);
            var lstTemp = dt.ToList<UserProfile>();
            return lstTemp;
        }

        public List<UserProfile> GetByGroup(int _iGroupID)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objResaleDAO = new UserProfileDAO(this.m_objData);
            dt = objResaleDAO.GetByGroup(_iGroupID);
            var lstTemp = dt.ToList<UserProfile>();
            return lstTemp;
        }

        public bool Insert(UserProfile usrProfile, UserMembership mebership, int GroupID)
        {
            try
            {
                this.BeginTran();

                UserProfileDAO objUserDAO = new UserProfileDAO(this.m_objData);
                int userID = objUserDAO.Insert(usrProfile.UserId,
                                                usrProfile.ManagerID,
                                                usrProfile.UserName,
                                                usrProfile.Name,
                                                usrProfile.Email,
                                                usrProfile.Phone,
                                                usrProfile.Birthday,
                                                usrProfile.Sex,
                                                usrProfile.Address,
                                                usrProfile.Avatar,
                                                usrProfile.Domain,
                                                usrProfile.IsLocked);

                UserMembershipDAO objMemberShipDAO = new UserMembershipDAO(this.m_objData);
                mebership.UserId = userID;
                mebership.Password = MDF.Utilites.Tools.CommonMethods.Encrypt(mebership.Password + mebership.PasswordSalt);
                objMemberShipDAO.Insert(mebership.UserId, mebership.Password, mebership.PasswordSalt, mebership.IsConfirmed);

                UserInGroupsDAO groupBUS = new UserInGroupsDAO(this.m_objData);
                groupBUS.Insert(userID, GroupID);

                this.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                this.RollbackTran();
                throw ex;
            }
        }

        public List<UserProfile> GetAll(string _strSearch, int _iUserGroupID, int _iManageID, int _iPage, int _iPageSize)
        {
            DataTable dt = new DataTable();
            UserProfileDAO objUserDAO = new UserProfileDAO(this.m_objData);
            dt = objUserDAO.GetAll(_strSearch,_iUserGroupID, _iManageID, _iPage, _iPageSize);
            var lstTemp = dt.ToList<UserProfile>();
            return lstTemp;
        }

        public void Delete(int iUserID, int DeletedUser)
        {
            UserProfileDAO objUserDAO = new UserProfileDAO(this.m_objData);
            objUserDAO.Delete(iUserID, DeletedUser);
        }

        public void Update(UserProfile usrProfile)
        {
            UserProfileDAO objUserDAO = new UserProfileDAO(this.m_objData);
            objUserDAO.Update(usrProfile.UserId,
                            usrProfile.ManagerID,
                            usrProfile.UserName,
                            usrProfile.Name,
                            usrProfile.Email,
                            usrProfile.Phone,
                            usrProfile.Birthday,
                            usrProfile.Sex,
                            usrProfile.Address,
                            usrProfile.Avatar,
                            usrProfile.Domain,
                            usrProfile.IsLocked);

        }

    }
}
